/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2008，所有权利保留。
 * 
 * 项目名：	rumba
 * 文件名：	CartridgeServiceImpl.java
 * 模块说明：	
 * 修改历史：
 * Feb 20, 2008 - lxm - 创建。
 */
package com.hd123.rumba.gwt.cartridge.server;

import java.util.ArrayList;
import java.util.List;

import com.hd123.rumba.cartridge.CartRumba;
import com.hd123.rumba.cartridge.biz.BCartridge;
import com.hd123.rumba.cartridge.biz.IBCartridgeMgr;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.gwt.base.client.exception.ClientBizException;
import com.hd123.rumba.gwt.base.server.RBRemoteServiceServlet;
import com.hd123.rumba.gwt.cartridge.client.CartridgeService;
import com.hd123.rumba.gwt.cartridge.client.biz.CCartridge;
import com.hd123.rumba.gwt.cartridge.client.biz.QCartridge;
import com.hd123.rumba.gwt.cartridge.server.biz.QCartridgeReader;
import com.hd123.rumba.runtime.NamingMgr;

/**
 * 构件包 | 远程服务 | 实现类
 * 
 * @author lxm
 * 
 */
public class CartridgeServiceImpl extends RBRemoteServiceServlet implements CartridgeService {

  public QCartridge getCoverCartridge() throws ClientBizException {
    try {
      BCartridge bcart = this.getCoverCart();
      assert bcart != null;
      QCartridgeReader reader = new QCartridgeReader();
      reader.setTarget(new QCartridge());
      reader.read(bcart);
      return reader.getTarget();
    } catch (BusinessException e) {
      this.reportError("无法取得" + CCartridge.CAPTION_CAPTION + "信息。", e);
      throw this.buildException("无法取得" + CCartridge.CAPTION_CAPTION + "信息。", e);
    }
  }

  public List getAllCartridges() throws ClientBizException {
    try {
      List list = new ArrayList();
      QCartridgeReader reader = new QCartridgeReader();
      List<BCartridge> bcarts = getCartMgr().getAllCartridges();
      for (BCartridge bcart : bcarts) {
        reader.setTarget(new QCartridge());
        reader.read(bcart);
        list.add(reader.getTarget());
      }
      return list;
    } catch (BusinessException e) {
      this.reportError("无法取得" + CCartridge.CAPTION_CAPTION + "信息。", e);
      throw this.buildException("无法取得" + CCartridge.CAPTION_CAPTION + "信息。", e);
    }
  }

  private IBCartridgeMgr cartMgr = null;

  private IBCartridgeMgr getCartMgr() throws BusinessException {
    if (cartMgr == null)
      cartMgr = (IBCartridgeMgr) NamingMgr.getInstance().lookupMgr(IBCartridgeMgr.class);
    return cartMgr;
  }

  private BCartridge getCoverCart() throws BusinessException {
    String prefix = CartridgeConstants.COVER_CART_PREFIX;
    if (prefix == null) {
      List<BCartridge> bcarts = getCartMgr().getAllCartridges();
      for (BCartridge bcart : bcarts)
        if (bcart.getAsCover().booleanValue()) {
          prefix = bcart.getPrefix();
          break;
        }
    }
    if (prefix == null)
      prefix = CartRumba.CART_PREFIX;
    return getCartMgr().getCartridgeByPrefix(prefix);
  }

}
